package com.hand.wfive.qmoor;

import android.os.Build;

import com.hand.wfive.App;
import com.hand.wfive.BuildConfig;
import com.hand.wfive.R;
import com.hand.wfive.loginAndVip.UserManager;
import com.hand.wfive.loginAndVip.model.User;
import com.m7.imkfsdk.KfStartHelper;
import com.moor.imkf.IMChatManager;
import com.moor.imkf.requesturl.RequestUrl;

import org.json.JSONException;
import org.json.JSONObject;

public class QmoorHelper {

    public static QmoorHelper getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final QmoorHelper INSTANCE = new QmoorHelper();
    }

    private KfStartHelper mHelper;
    private final String accessId = "6a4cc080-44e1-11ef-926f-0fa78045b502";

    private QmoorHelper() {
        initKfHelper();
    }

    private void initKfHelper() {
        // 初始化help
        mHelper = KfStartHelper.getInstance();
        // 设置服务环境 阿里/腾讯/华为,可以联系技术支持确认您的服务环境
        // 注意要在helper.initSdkChat()之前设置
        // 阿里云环境：RequestUrl.ALIYUN_REQUEST
        // 腾讯云环境：RequestUrl.TENCENT_REQUEST
        // 华为云环境：RequestUrl.HUAWEI_REQUEST
        RequestUrl.setRequestBasic(RequestUrl.TENCENT_REQUEST);
        // 配置图片加载器
        mHelper.setImageLoader(new GlideImageLoader());
    }

    public void initSdk() {
        User user = UserManager.getInstance().getCurUser();
        // 添加配置拓展参数，如需使用注意要在initSdkChat之前之前用
        setOtherParams(user);
        // 设置参数：初始化sdk方法，必须先调用该方法进行初始化后才能使用IM相关功能
        // accessId 接入id（需后台配置获取）
        // userName 用户名 (非空)
        // userId   用户id (非空)
        String userName = UserManager.LoginType_normal.equals(user.getLoginType())
                ? user.getUsername() : user.getNickName();
        mHelper.initSdkChat(accessId, userName, user.getId());
    }

    /**
     * 获取未读消息数
     */
    public void getUnReadCount(IMChatManager.HttpUnReadListen listen) {
        if (!UserManager.getInstance().isLogin()) return;
        // 在initSdk后调用
        // 调用此方法前也需要先配置服务:RequestUrl.setRequestUrl
        User user = UserManager.getInstance().getCurUser();
        String userName = UserManager.LoginType_normal.equals(user.getLoginType())
                ? user.getUsername() : user.getNickName();
        IMChatManager.getInstance().getMsgUnReadCountFromService(accessId, userName, user.getId(), listen);
    }

    /**
     * 添加拓展参数，注意要在 initSdkChat之前之前用
     */
    private void setOtherParams(User user) {
        try {
            JSONObject otherObject = new JSONObject();
            otherObject.put("包名", BuildConfig.APPLICATION_ID);
            otherObject.put("App版本", "v" + BuildConfig.VERSION_NAME);
            otherObject.put("App名称", App.getContext().getString(R.string.app_name));
            otherObject.put("公司", App.getContext().getString(R.string.companyname));
            otherObject.put("渠道", App.getContext().getString(R.string.channel));
            otherObject.put("手机型号", Build.MODEL);
            otherObject.put("手机类型", Build.BRAND);
            otherObject.put("手机系统版本", Build.VERSION.RELEASE);
            otherObject.put("AndroidSDK", Build.VERSION.SDK_INT);
            otherObject.put("用户ID", user.getId());
            otherObject.put("用户名", user.getUsername());
            otherObject.put("用户昵称", user.getNickName());
            otherObject.put("会员用户", UserManager.getInstance().isVip() ? "是" : "不是");
            otherObject.put("用户登录方式", getLoginType(user.getLoginType()));
            IMChatManager.getInstance().setUserOtherParams("", otherObject, false, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String getLoginType(String loginType) {
        switch (loginType) {
            case "1":
                return "账号登录";
            case "2":
                return "微信登录";
            case "3":
                return "QQ登录";
            case "4":
                return "华为账号登录";
            case "5":
                return "手机号登录";
            case "6":
                return "验证码登录";
            default:
                return "未知";
        }
    }
}
